package nl.saxion.act.playground;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public abstract class TileView extends View {
	private static final String TAG = "TileView";

	private static final int FIXED_GRID = 1;
	private static final int VARIABLE_GRID = 0;

	private static final int GAMEMODE = FIXED_GRID;
	private static final int TILESIZE = 2;

	protected int mXTileCount = 24;
	protected int mYTileCount = 12;

	protected int mTileSize = TILESIZE;

	/**
	 * There is a border around the tile grid mXOffset x-offset in pixels:
	 * x-offset of first tile mYOffset y-offset in pixels: y-offset of first
	 * tile
	 */
	protected int mXOffset;
	protected int mYOffset;

	/**
	 * A hash that maps integer handles specified by the subclasser to the
	 * drawable that will be used for that reference
	 */
	private Bitmap[] mTileArray;
	private Bitmap[] customGreenArray;
	/**
	 * A two-dimensional array of integers in which the number represents the
	 * index of the tile in the mTileArray that should be drawn at that location
	 */
	private int[][] mTileGrid;

	private final Paint mPaint = new Paint();

	public TileView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mTileArray = new Bitmap[300];
		createGreenTiles();
		mTileSize = TILESIZE;
		mTileGrid = new int[mXTileCount][mYTileCount];
		clearTiles();
		Log.d(TAG, "initTileView( " + mTileSize + " )");
	}

	public TileView(Context context, AttributeSet attrs) {
		super(context, attrs);

		mTileSize = TILESIZE;
		mTileGrid = new int[mXTileCount][mYTileCount];
		clearTiles();
	}

	/**
	 * Rests the internal array of Bitmaps used for drawing tiles, and sets the
	 * maximum index of tiles to be inserted
	 * 
	 * @param tilecount
	 *            the maximum number of tiles to be drawn
	 */

	public void resetTiles(int tilecount) {
		mTileArray = new Bitmap[tilecount];
	}

	/**
	 * Function to set the specified Drawable as the tile for a particular
	 * integer key.
	 * 
	 * @param key
	 * @param tile
	 */
	public void loadTile(int key, Drawable tile) {
		Bitmap bitmap = Bitmap.createBitmap(mTileSize, mTileSize,
				Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		tile.setBounds(0, 0, mTileSize, mTileSize);
		tile.draw(canvas);
		mTileArray[key] = bitmap;
	}

	public void createGreenTiles() {
		Drawable tile = r.getDrawable(R.drawable.greenballtop);


		Bitmap bitmap = Bitmap.createBitmap(mTileSize, mTileSize,
				Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		tile.setBounds(0, 0, mTileSize, mTileSize);
		tile.draw(canvas);

		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);

		Paint mTextPaint = new Paint (Paint.ANTI_ALIAS_FLAG);
		final float densityMultiplier = getContext().getResources()
				.getDisplayMetrics().density;  
		mTextPaint.setTextSize(12.0f*densityMultiplier);

		float size = mTextPaint.getTextSize();	
		String s = "" + 18;
		canvas.drawText(s, 10, 25, mTextPaint);

		mTileArray[18] = bitmap;	





	}

	private Resources r = this.getContext().getResources();

	/**
	 * Resets all tiles to 0 (empty)
	 * 
	 */
	public void clearTiles() {
		for (int x = 0; x < mXTileCount; x++) {
			for (int y = 0; y < mYTileCount; y++) {
				setTile(0, x, y);
			}
		}
	}

	/**
	 * Used to indicate that a particular tile (set with loadTile and referenced
	 * by an integer) should be drawn at the given x/y coordinates during the
	 * next invalidate/draw cycle.
	 * 
	 * @param tileindex
	 * @param x
	 * @param y
	 */
	public void setTile(int tileindex, int x, int y) {
		mTileGrid[x][y] = tileindex;
	}


	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int x = (int) ((event.getX() - mXOffset) / mTileSize);
		int y = (int) ((event.getY() - mYOffset) / mTileSize);
		if (x < mXTileCount && y < mYTileCount
				&& (event.getX() - mXOffset) >= 0
				&& (event.getY() - mYOffset) >= 0) { /* Game Board touched */

			touched(x, y);
		}
		return super.onTouchEvent(event);
	}

	/**
	 * method to handle tile touch.
	 * 
	 * @param x
	 *            the x-coordinate of the tile touched
	 * @param y
	 *            the y-coordinate of the tile touched
	 */
	public abstract void touched(int x, int y);

	@SuppressWarnings("unused")
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {

		if (w == 0 || h == 0)
			return;

		if (GAMEMODE == VARIABLE_GRID) { /*
		 * size of grid depends on tile and
		 * screen size
		 */
			mXTileCount = (int) Math.floor(w / mTileSize);
			mYTileCount = (int) Math.floor(h / mTileSize);

		} else if (GAMEMODE == FIXED_GRID) { /*
		 * size of grid is fixed and
		 * tileSize is computed
		 */
			mTileSize = Math.min((int) Math.floor(w / mXTileCount),
					(int) Math.floor(h / mYTileCount));

		}
		mXOffset = ((w - (mTileSize * mXTileCount)) / 2);
		mYOffset = ((h - (mTileSize * mYTileCount)) / 2);

		mTileGrid = new int[mXTileCount][mYTileCount];
		clearTiles();
	}

	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		for (int x = 0; x < mXTileCount; x++) {
			for (int y = 0; y < mYTileCount; y++) {
				if (mTileGrid[x][y] > 0) {
					canvas.drawBitmap(mTileArray[mTileGrid[x][y]], mXOffset + x
							* mTileSize, mYOffset + y * mTileSize, mPaint);
				}
			}
		}

	}

}
